<?php

// a class for d3forum comment integration
class d3downloadsComment extends D3commentAbstract {

	function fetchSummary( $external_link_id )
	{
		$db =& Database::getInstance() ;
		$myts =& MyTextSanitizer::getInstance();

		$module_handler =& xoops_gethandler( 'module' ) ;
		$module =& $module_handler->getByDirname( $this->mydirname ) ;

		$down_id = intval( $external_link_id ) ;
		$mydirname = $this->mydirname ;
		if( preg_match( '/[^0-9a-zA-Z_-]/' , $mydirname ) ) die( 'Invalid mydirname' ) ;

		$content_row = $db->fetchArray( $db->query( "SELECT lid, cid, title, description FROM ".$db->prefix( $mydirname."_downloads" )." WHERE lid=$down_id" ) ) ;
		if( empty( $content_row ) ) return '' ;

		$cid = intval( $content_row['cid'] );
		$uri = XOOPS_URL.'/modules/'.$mydirname.'/index.php?page=singlefile&cid='.$cid.'&lid='.$down_id;

		$str = strip_tags($myts->displayTarea(strip_tags( $content_row['description'] ) ) );
		$summary = xoops_substr( $str , 0 , 255 );

		return array(
			'dirname' => $mydirname ,
			'module_name' => $module->getVar( 'name' ) ,
			'subject' => $myts->makeTboxData4Show( $content_row['title'] ) ,
			'uri' => $uri,
			'summary' => $summary,
		) ;
	}

	function validate_id( $link_id )
	{
		require_once dirname(dirname(__FILE__)).'/include/common_functions.php' ;
		$down_id = intval( $link_id ) ;
		$mydirname = $this->mydirname ;

		$db =& Database::getInstance() ;
		$whr_cat = "cid IN (".implode(",",d3download_can_read( $mydirname ) ).")" ;
		list( $count ) = $db->fetchRow( $db->query( "SELECT COUNT(*) FROM ".$db->prefix( $mydirname."_downloads" )." WHERE lid=$down_id AND visible = '1' AND ( $whr_cat )" ) ) ;
		if( $count <= 0 ) return false ;
		else return $down_id ;
	}

	function onUpdate( $mode , $link_id , $forum_id , $topic_id , $post_id = 0 )
	{
		$down_id = intval( $link_id ) ;
		$mydirname = $this->mydirname ;

		$db =& Database::getInstance() ;

		list( $count ) = $db->fetchRow( $db->query( "SELECT COUNT(*) FROM ".$db->prefix($this->d3forum_dirname."_posts")." p LEFT JOIN ".$db->prefix($this->d3forum_dirname."_topics")." t ON t.topic_id=p.topic_id WHERE t.forum_id=$forum_id AND t.topic_external_link_id='$down_id'" ) ) ;
		$db->queryF( "UPDATE ".$db->prefix( $mydirname."_downloads" )." SET comments=$count WHERE lid=$down_id" ) ;

		return true ;
	}

}

?>